package ui.swing.tree.filesystem;

import java.io.File;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;


public class FileSystemTreeModel extends DefaultTreeModel {

	private static final long serialVersionUID = -4382723548184294162L;
	
	private boolean directoryOnly = true;
	
	private DefaultMutableTreeNode rootNode;

	public FileSystemTreeModel() {
		super(new DefaultMutableTreeNode(new FileNode("我的电脑")));
		rootNode = (DefaultMutableTreeNode)getRoot();
		init();
	}
	
	void init() {
		rootNode.removeAllChildren();
		File[] sysRoots = File.listRoots();
		for(File aRoot : sysRoots)
		{
			FileNode fInfo = new FileNode(aRoot, aRoot.getPath());
			DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(fInfo);
			rootNode.add(fileNode);
		}
		// 注意一定要有这个
		reload();
	}
	
	public void loadFileSystemPath(DefaultMutableTreeNode node) {
		Object userObj = node.getUserObject();
		if(!(userObj instanceof FileNode)) {
			return;
		}
		FileNode fNode = (FileNode)userObj;
		if(fNode.isVirtual()) {
			return;// TODO
		}
		File file = fNode.getFile();
		if(!file.isDirectory()) // 对于一些没放关盘的光驱，也是有用的
			return;
		File[] subFiles = file.listFiles();
		if(subFiles == null)
			return;
		for(File aFile : subFiles) {
			if(!aFile.isDirectory() && directoryOnly) {
				continue;
			}
			FileNode newFileNode = new FileNode(aFile);
			DefaultMutableTreeNode newTreeNode = new DefaultMutableTreeNode(newFileNode);
			newTreeNode.setAllowsChildren(aFile.isDirectory());
			node.add(newTreeNode);
		}
	}

	public DefaultMutableTreeNode findNodeByFile(File file){
		if(!file.exists()) {
			return null;
		}

		String strFileAbsPath = file.getAbsolutePath();
		if(!file.isDirectory())
			strFileAbsPath = strFileAbsPath.substring(0, strFileAbsPath.length() - file.getName().length());
		String[] arrFilePath = strFileAbsPath.split("\\\\");
		if(arrFilePath != null) {
			DefaultMutableTreeNode curTreeNode = rootNode;
			for(String pathString : arrFilePath) {
				//pathString+="\\";
				boolean foundChild = false;
				for(int i = 0, times = curTreeNode.getChildCount(); i < times; i++) {
					DefaultMutableTreeNode child = (DefaultMutableTreeNode)curTreeNode.getChildAt(i);
					Object userObj = child.getUserObject();
					if(!(userObj instanceof FileNode)) {
						continue;
					}
					FileNode fNode = (FileNode)child.getUserObject();
					
					String fName = fNode.getFile().getName();
					if("".equals(fName)) { // 驱动器节点是这种情况
						fName = fNode.getFile().getPath().substring(0, fNode.getFile().getPath().length() - 1);
					}
					if(!fName.equalsIgnoreCase(pathString)) {
						continue;
					}
					else {
						curTreeNode = child; // 如果找到，进去子节点继续找
						foundChild = true;
						break;
					}
				}
				if(foundChild == false){ // 在当前树中未找到文件所在的路径
					return curTreeNode;
				}
			}
			return curTreeNode; // 如果执行到这里，说明外层循环结束，树中存在文件所在的全路径
		}
		else {
			return null;
		}
	}

	public boolean isDirectoryOnly() {
		return directoryOnly;
	}

	public void setDirectoryOnly(boolean directoryOnly) {
		this.directoryOnly = directoryOnly;
	}
}

